<%@ page language="java" import="java.util.*,java.text.*,javax.servlet.*" pageEncoding="utf-8"%>
<%@page import="issuedfileaction.DBConnection;"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>       
    <title>登陆数据收取</title>    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>  
  <body> 
  <%
      DBConnection db=new DBConnection();
      SimpleDateFormat sdf=new SimpleDateFormat("-MM");
	  SimpleDateFormat sdft=new SimpleDateFormat("yyyy-MM");
	  SimpleDateFormat sdfb=new SimpleDateFormat("yyyy-MM-dd");
      String monthday=db.SQLExec("select optionvalue from orgoption where optionname='ROOLBACK_MONTHDAY' ");
      String beginmonth=db.SQLExec("select optionvalue from orgoption where optionname='ROOLBACK_BEGINMONTH' ");
      String endmonth=db.SQLExec("select optionvalue from orgoption where optionname='ROOLBACK_ENDMONTH' ");
      String MonthBeginDate="";
      String MonthEndDate="";
      
      String currentmonth=sdf.format(new Date());
	  String currentyear=sdft.format(new Date());	      
	  currentmonth=currentmonth.replace("-0", "");
	  currentmonth=currentmonth.replace("-", "");
	  MonthEndDate=currentyear+"-"+endmonth;
	  if(sdfb.parse(MonthEndDate).after(sdfb.parse(sdfb.format(new Date())))){
	    	  //如果设置的最后日期在当前日期之后，要减一个月
		 Calendar d=Calendar.getInstance();
	     d.setTime(sdfb.parse(MonthEndDate));
	     d.add(d.MONTH, -1);
	     MonthEndDate=sdft.format(d.getTime())+"-"+endmonth;

	  }
	  if(Integer.parseInt(beginmonth)>=Integer.parseInt(endmonth)){	    	  
	     //如果设置的开始日大于结束日,要减一个月
		 Calendar d=Calendar.getInstance();
		 d.setTime(sdfb.parse(MonthEndDate));
		 d.add(d.MONTH, -1);
		 MonthBeginDate=sdft.format(d.getTime())+"-"+beginmonth;
	  }else{
	     MonthBeginDate=sdft.format(sdft.parse(MonthEndDate))+"-"+beginmonth;
	  }

	  Calendar endca1 = Calendar.getInstance();	    
	  endca1.setTime(sdfb.parse(MonthEndDate));
	  Calendar beginca1 = Calendar.getInstance();	      
	  beginca1.setTime(sdfb.parse(MonthBeginDate));
	  Date endca = endca1.getTime(); 
	  Date beginca = beginca1.getTime();
	  long diffday=Math.abs((endca.getTime()-beginca.getTime())/(24*60*60*1000));
	  int k=Integer.parseInt(String.valueOf(diffday));
	  if(k<Integer.parseInt(monthday)){//如果天数大于结束日期与开始日期之差，天数取差，不取设置的天数
	     monthday=String.valueOf(k);  
	  }      
      //String BeginTime=request.getParameter("begintime");
      //String EndTime=request.getParameter("endtime");
      //BeginTime=sdfb.format(sdfb.parse(BeginTime));
      //EndTime=sdfb.format(sdfb.parse(EndTime));
      String BeginTime="2012-07-06";//sdf.format(new Date());
      String EndTime="2012-07-07";//sdf.format(new Date());      
      String CustomerName="";
      String StandMan="";
      String TransType="";
      String Province="";
      String FlowState="";
      //采购行，数量      
      db.SQLUpdateLinesAndQty("call sysdatacontrastsourcedatapur(?,?,?)",BeginTime,EndTime,"PM");
      //销售行，数量
      db.SQLUpdateLinesAndQty("call sysdatacontrastsourcedatasales(?,?,?)",BeginTime,EndTime,"SM");
      //库存行，数量
      db.SQLUpdateLinesAndQty("call sysdatacontrastsourcedatastock(?,?,?)",BeginTime,EndTime,"IM");
      //文件状态
      db.SQLUpdateMonthState("call getmonthdatastate(?,?,?,?,?)",BeginTime,EndTime,sdfb.format(endca),String.valueOf(k),monthday); //k回滚天数，monthday没传天数     
      try{
         CustomerName=new String(request.getParameter("customername").getBytes("ISO-8859-1"),"utf-8");
      }catch(Exception e){
         CustomerName="";
      }      
      try{
         StandMan=new String(request.getParameter("standman").getBytes("ISO-8859-1"),"utf-8");
      }catch(Exception e){
         StandMan="";
      }            
      try{
         TransType=new String(request.getParameter("transtype").getBytes("ISO-8859-1"),"utf-8");
      }catch(Exception e){
         TransType="";
      }            
      try{
         Province=new String(request.getParameter("province").getBytes("ISO-8859-1"),"utf-8");
      }catch(Exception e){
         Province="";
      }      
      try{
         FlowState=new String(request.getParameter("flowstate").getBytes("ISO-8859-1"),"utf-8");
      }catch(Exception e){
         FlowState="";
      }            
      String[] FlowStateArray=FlowState.split(",");
      String TransPurChase="NA";
      String TransSale="NA";
      String TransStock="NA";
      for(int i=0;i<FlowStateArray.length;i++){
          if(FlowStateArray[i].equals("采购")){
             TransPurChase="Y";
          }else if(FlowStateArray[i].equals("销售")){
             TransSale="Y";
          }else if(FlowStateArray[i].equals("库存")){
             TransStock="Y";
          }
      }            
      List<Object> ListRows=new ArrayList<Object>();
      ListRows=db.SQLProListExec("{call MONTHCONSOLE.PRO_MONTHREAD(?,?,?,?,?,?,?,?,?,?,?,?,?)}",BeginTime,EndTime,CustomerName,StandMan,TransType,Province,TransPurChase,TransSale,TransStock);
	  HttpSession httpSession = request.getSession();         
      httpSession.setAttribute("MonthListRows", ListRows);	
      httpSession.setAttribute("MonthBeginTime",BeginTime);
      httpSession.setAttribute("MonthEndTime",EndTime);
      httpSession.setAttribute("MonthCustomerName",CustomerName);
      httpSession.setAttribute("MonthStandMan",StandMan);
      httpSession.setAttribute("MonthTransType",TransType);
      httpSession.setAttribute("MonthProvince",Province);
      httpSession.setAttribute("MonthFlowState",FlowState);              		    	  
	  response.sendRedirect("MonthMonitor.jsp");     
  %>
  </body>
</html>